package com.dsg.controller.oil;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.DsgStatusConstant;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.UUIDGenerator;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.oil.DsgOilAccount;
import com.dsg.service.oil.IDsgOilAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/oil/dsgOilAccount")
@Slf4j
@Api(description = "油公司账户")
public class DsgOilAccountController extends BaseController {

    @Autowired
    private IDsgOilAccountService dsgOilAccountService;

    /**
     * 查询列表（分页）
     *
     * @param dsgOilAccount
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查询列表（分页）")
    public AjaxResult list(DsgOilAccount dsgOilAccount) {
        Page page = getPage();  // 分页
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("accountNo");
            orderItem.setAsc(false);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgOilAccount.setEntCode(SingletonLoginUtils.getSystemEntCode());
        IPage<DsgOilAccount> dsgOilAccountIPage = dsgOilAccountService.selectByList(page, dsgOilAccount);
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilAccountIPage.getRecords(), (int) page.getTotal()); // 格式要返回的数据
        return ajaxResult;
    }

    /**
     * 查询单条数据
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}/view")
    @ApiOperation("查询单条数据")
    public AjaxResult view(@PathVariable String id) {
        DsgOilAccount dsgOilAccount = dsgOilAccountService.getById(id);
        return json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilAccount);
    }

    /**
     * 新增
     *
     * @param dsgOilAccount
     * @return
     */
    @NoRepeatSubmit
    @PostMapping(value = "/add")
    @ApiOperation("新增")
    public AjaxResult add(@RequestBody DsgOilAccount dsgOilAccount) {
        if (StringUtil.isEmpty(dsgOilAccount.getAccountNo())) {
            return setErrorJson("账户编号不能為空！");
        }
        //查询账户编号是否存在
        QueryWrapper<DsgOilAccount> entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("ACCOUNT_NO", dsgOilAccount.getAccountNo());
        entityWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
        if (dsgOilAccountService.count(entityWrapper) > 0) {
            return setErrorJson("账户编号：" + dsgOilAccount.getAccountNo() + "已存在！");
        }
        entityWrapper = new QueryWrapper<>();
        entityWrapper.eq("PID", dsgOilAccount.getPid());
        if (dsgOilAccountService.count(entityWrapper) > 0) {
            dsgOilAccount.setIsDefault(DsgStatusConstant.IS_DEFAULT_ZERO);
        } else {
            dsgOilAccount.setIsDefault(DsgStatusConstant.IS_DEFAULT_ONE);
        }
        dsgOilAccount.setOid(UUIDGenerator.getUUID());
        boolean flag = dsgOilAccountService.save(dsgOilAccount);
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 更新
     *
     * @param dsgOilAccount
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgOilAccount dsgOilAccount) {
        AjaxResult ajaxResult;
        if (StringUtil.isEmpty(dsgOilAccount.getAccountNo())) {
            ajaxResult = setErrorJson("账户编号不能為空！");
        } else {
            //查询账户编号是否存在
            QueryWrapper<DsgOilAccount> entityWrapper = new QueryWrapper<>();
            entityWrapper.eq("ACCOUNT_NO", dsgOilAccount.getAccountNo());
            entityWrapper.eq("ENT_CODE", SingletonLoginUtils.getSystemEntCode());
            entityWrapper.ne("OID", dsgOilAccount.getOid());
            if (dsgOilAccountService.count(entityWrapper) > 0) {
                ajaxResult = setErrorJson("账户编号：" + dsgOilAccount.getAccountNo() + ",已经存在！");
            } else {
                boolean flag = dsgOilAccountService.updateById(dsgOilAccount);
                if (flag) {
                    ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, dsgOilAccount);
                } else {
                    ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
                }
            }
        }
        return ajaxResult;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @DeleteMapping(value = "/delete")
    @ApiOperation("批量删除")
    public AjaxResult delete(@ApiParam(value = "id以，号分隔", name = "idList", required = true) @RequestParam String idList) {
        Boolean flag = dsgOilAccountService.removeByIds(Arrays.asList(idList.split(",")));
        if (flag) {
            return result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }

    /**
     * 設置默認賬戶
     *
     * @param id
     * @return
     */
    @PutMapping(value = "/updateDefault")
    @ApiOperation("設置默認賬戶")
    public ResponseEntity updateDefault(@ApiParam(value = "id", name = "id", required = true) @RequestParam String id) {
        AjaxResult ajaxResult;
        boolean flag = dsgOilAccountService.updateDefault(id);
        if (flag) {
            ajaxResult = result(MessageConstants.SSO_STATUS_SUCCESS);
        } else {
            ajaxResult = result(MessageConstants.SSO_STATUS_FAIL);
        }
        return ResponseEntity.ok(ajaxResult);
    }
}
